package cn.dceast.algorithm.controller;

import cn.dceast.algorithm.model.EyeAiLibrary;
import cn.dceast.algorithm.model.TsData;
import cn.dceast.algorithm.service.AlgorithmService;
import cn.dceast.common.data.RtnData;
import cn.dceast.former.model.EyeModelData;
import cn.dceast.user.model.UserInfo;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yang_ on 2017/7/26.
 */
@Controller
@RequestMapping("/algorithmController")
@Api(tags = {"algorithmController"})
public class AlgorithmController {

    @Autowired
    private AlgorithmService algorithmService;

    @ApiOperation(value = "算法列表", notes = "算法列表")
    @RequestMapping(value = "/getAlgorithmList.do", method = RequestMethod.GET)
    @ResponseBody
    public RtnData getAlgorithmList(String modelId,String aiName,String aiType) {
        Map<String, Object> args = new HashMap<>();
        args.put("aiName",aiName);
        args.put("aiType",aiType);
        List<EyeAiLibrary> list = algorithmService.getAlgorithms(args);//所有算法列表
        List<EyeAiLibrary> currentAist = algorithmService.getCurrentAlgorithms(Integer.valueOf(modelId));//所有算法列表
        Map<String, Object> resMap = new HashedMap();
        resMap.put("list", list);
        resMap.put("currentAist", currentAist);
        return RtnData.ok(resMap);
    }

    /**
     * 获取所有算法类型
     * @return
     */
    @RequestMapping(value = "/getAlgorithmType.do", method = RequestMethod.GET)
    @ResponseBody
    public RtnData getAlgorithmType() {
        List<Map<String,Object>> res=algorithmService.getAlgorithmType();
        return RtnData.ok(res);
    }

    @RequestMapping(value = "/addAlgorithm2model.do", method = RequestMethod.POST)
    @ResponseBody
    public RtnData addAlgorithm2model(String modelId, String AIId) {
        if (StringUtils.isEmpty(modelId)) {
            return RtnData.fail("模型id为空");

        }
        EyeModelData eyeModelData = new EyeModelData();
        eyeModelData.setId(Integer.valueOf(modelId.trim()));
        eyeModelData.setModelAlgorithm(AIId);
        if (algorithmService.updataModel(eyeModelData) > 0) {
            return RtnData.ok("操作成功");
        } else {
            return RtnData.fail("操作失败");
        }
    }

    @RequestMapping(value = "/getTSData.do")
    @ResponseBody
    public RtnData getTSData(String modelId, String tableName) {

        List<TsData> tsDatas=algorithmService.getTSColum("upload_file_data_1501059310890");
        return RtnData.ok(tsDatas);

    }
    @RequestMapping(value = "/dealTSData.do")
    @ResponseBody
    public RtnData dealTSData(String modelId, String tableName) {

        List<TsData> tsDatas=algorithmService.getTSColum("upload_file_data_1501059310890");
        return RtnData.ok(tsDatas);

    }

    /**
     * 获取所有字段
     * @param modelId
     * @return
     */
    @RequestMapping(value = "/inputSetTSData.do")
    @ResponseBody
    public RtnData inputSetTSData(String modelId) {
        Map<String,Object> args = new HashedMap();
        args.put("modelId",modelId);
        List<Map<String,Object>> res=algorithmService.getAllInputCol(args);
        return RtnData.ok(res);
    }

    /**
     * 根据输入项生成算法处理表（也是结果表）
     * @param modelId
     * @param colIds
     * @return
     */
    @RequestMapping(value = "/doInputSetTSData.do")
    @ResponseBody
    public RtnData doInputSetTSData(String modelId, String colIds,String userId) {
        System.out.println(colIds);
        Gson gson = new Gson();
        List<Map> list=gson.fromJson(colIds, List.class);
        List<String> inputCols=new ArrayList<>();
        try {
            algorithmService.updataModelUploadCloum(list);
        } catch (Exception e) {
            e.printStackTrace();
            return RtnData.fail("设置输入项失败");
        }
        for(Map map:list){
            if (map.get("value").equals("1")){
                inputCols.add(map.get("id").toString());
            }
        }
        if (inputCols.size()<=0){
            return RtnData.fail("没有输入项");
        }
        Map<String,Object> args = new HashedMap();
        args.put("modelId",modelId);
        args.put("colId",inputCols);
        String resTable=algorithmService.doInputCol2Res(args);
        return RtnData.ok(resTable);
    }
    /**
     * 使用模型生成算法处理表（也是结果表）
     * @param modelId
     * @param
     * @return
     */
    @RequestMapping(value = "/doInputSetTSDataByUseModel.do")
    @ResponseBody
    public RtnData doInputSetTSDataByUseModel(String modelId,String userId) {

        List<String> inputCols=algorithmService.getModelInputCol(modelId);

        if (inputCols.size()<=0){
            return RtnData.fail("没有输入项");
        }
        Map<String,Object> args = new HashedMap();
        args.put("modelId",modelId);
        args.put("colId",inputCols);
        String resTable=algorithmService.doInputCol2Res(args);
        return RtnData.ok(resTable);
    }

    /**
     * 获取算法要设置的参数
     * @param aiId
     * @return
     */
    @RequestMapping(value = "/getAlgorithmArgs.do")
    @ResponseBody
    public RtnData getAlgorithmArgs(String modelId,String aiId,HttpSession session) {

        Integer userId = (Integer)session.getAttribute("userid");
        if (null != userId){
            Map<String,Object> param = new HashedMap();
            param.put("userId",userId);
            param.put("modelId",modelId);
            param.put("aiId",aiId);
            Map<String,Object> aiArgmap=algorithmService.getAiArgmap(param);
            if (aiArgmap==null){
                aiArgmap = new HashedMap();
                String resaiArgs=algorithmService.getAlgorithmArgs(aiId);
                aiArgmap.put("aiArgs",resaiArgs);
            }
            return RtnData.ok(aiArgmap);
        }else{
            return RtnData.fail("请先登录");
        }

    }
    /**
     * 不同用户设置算法参数
     * @param
     * @return
     */
    @RequestMapping(value = "/doSetAlgorithmArgs.do")
    @ResponseBody
    public RtnData doSetAlgorithmArgs(String modelId,String aiArgs,String aiId,String aiMapId,HttpSession session) {

        Integer userId = (Integer)session.getAttribute("userid");
        if (null != userId){
            Map<String,Object> param = new HashedMap();
            param.put("userId",userId);
            param.put("modelId",modelId);
            param.put("aiArgs",aiArgs);
            param.put("aiId",aiId);
            param.put("aiMapId",aiMapId);
            if(StringUtils.isEmpty(aiMapId)){
                int res=algorithmService.doSetAlgorithmArgs(param);
            }else {
                int res=algorithmService.updataSetAlgorithmArgs(param);
            }
            return RtnData.ok("");
        }else{
            return RtnData.fail("请先登录");
        }

    }
}
